<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;

class MemberController extends AdminController {
	public function _initialize(){
        parent::_initialize();
    }
	
	 /**
     * 会员列表
     */
    public function index(){
    	$paixu=I('paixu');
    	if(isset($_GET['gtc'])){
    		//dump($_GET);
    		if($_GET['gtc']=='desc'){
    			$paixu="money desc";
    			$_SESSION['gtcsx']="asc";
    		}else{
    			$paixu="money asc";
    			$_SESSION['gtcsx']="desc";
    		}
    	}
    	 if(isset($_GET['sheng'])){
    		//dump($_GET);
    		if($_GET['sheng']=='desc'){
    			$paixu="area_sheng desc";
    			$_SESSION['sheng']="asc";
    		}else{
    			$paixu="area_sheng asc";
    			$_SESSION['sheng']="desc";
    		}
    	} 
    	if(isset($_GET['shi'])){
    		//dump($_GET);
    		if($_GET['shi']=='desc'){
    			$paixu="area_shi desc";
    			$_SESSION['shi']="asc";
    		}else{
    			$paixu="area_shi asc";
    			$_SESSION['shi']="desc";
    		}
    	}
    	if(isset($_GET['cny'])){
    		//dump($_GET);
    		if($_GET['cny']=='desc'){
    			$paixu="jifen desc";
    			$_SESSION['cnysx']="asc";
    		}else{
    			$paixu="jifen asc";
    			$_SESSION['cnysx']="desc";
    		}
    	}
    	
		//批量删除处理
		$id = I('id');
		//dump($id);die;
		if(!empty($id) && is_array($id)){
			$delete['id'] = array('in',$id);
			$re = M('Member')->where($delete)->setField('status',-1);
			if(!$re){
				$this->error('删除失败');
			}
		}
		
		//查询条件处理
		$realname_post = I('realname');
		$nickname_post = I('nickname');
		$type = I('type');
		$post = array('type'=>$type,'realname'=>$realname_post);
		
		
		if(!empty($realname_post)){
			$where['realname'] = array('like',"%{$realname_post}%");
		}
		if(!empty($nickname_post)){
			$where['nickname'] = array('like',"%{$nickname_post}%");
		}
		
		
		//分页处理
		$post_param = $where;
		$post_param['order'] = $order;
		
		$count = M("Member")->where($where)->count();
		$Page  = new \Think\Page($count,20);
		setPageParameter($Page, $post_param);
		$show = $Page->show();
		$list = M('Member')->where($where)->order($paixu)->limit($Page->firstRow.','.$Page->listRows)->select();
		
		
		
		//赋值给页面上
		$this->assign('post',$post);
		$this->assign('count',$count);
		$this->assign('school_type',$school_type);
		$this->assign('school_name',$school_name);
		$this->assign('grade',$grade);
		$this->assign('class',$class);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
		 
    }
	
	public function del(){
		$id = I('id');
		
		$id = intval($id);
		if(!empty($id)){
			$re = M('Member')->where("id={$id}")->setField('status',-1);
			if($re){
				$this->success('删除成功',U('Member/index'));
			}else{
				$this->error('删除失败');
			}
		}else{
			$this->error('删除失败');
		}
	}
	
	public function save(){
		$id = I('id');
		
		$id = intval($id);
		
			$user = M('Member')->where("id='{$id}'")->find();
			//格式化班级信息
			$school_type_post = I('school_type')?I('school_type'):$user['school_type'];
			$school_post = I('school')?I('school'):$user['school'];
			$grade_post = I('grade')?I('grade'):$user['grade'];
			$class_post = I('class')?I('class'):$user['class'];
			$realname_post = I('realname')?I('realname'):$user['realname'];
			$classno_post = I('classno')?I('classno'):$user['classno'];
			$post = array('school_type'=>$school_type_post,'school'=>$school_post,'grade'=>$grade_post,'class'=>$class_post,'realname'=>$realname_post,'classno'=>$classno_post);

			$school = M('Type')->Field('id,parent_id,name')->select();
			foreach($school as $k=>$v){
				if($v['parent_id'] == 0){
					$school_type[] = $v;
				}
				if(!empty($school_type_post) && $v['parent_id'] == $school_type_post){
					
					$school_name[] = $v;
				}
				if(!empty($school_post) && $v['parent_id'] == $school_post){
					$grade[] = $v;
				}
				if(!empty($grade_post) && $v['parent_id'] == $grade_post){
					$class[] = $v;
				}
				$school[$v['id']] = $v['name'];
			}
			
			$this->assign('post',$post);
			$this->assign('school_type',$school_type);
			$this->assign('school_name',$school_name);
			$this->assign('grade',$grade);
			$this->assign('class',$class);
			
			$this->assign('user',$user);
			$this->display();
		
	}
	
	//保存修改信息
	public function sub(){
		if(IS_AJAX){
			$school_type_post = I('school_type');
			$school_post = I('school');
			$grade_post = I('grade');
			$class_post = I('class');
			$realname_post = I('realname');
			$classno_post = I('classno');
			$post = array('school_type'=>$school_type_post,'school'=>$school_post,'grade'=>$grade_post,'class'=>$class_post,'realname'=>$realname_post,'classno'=>$classno_post);
			$id = I('id');
			$id = intval($id);
			foreach($post as $v){
				if(empty($v)){
					$data['status'] = -2;
					$data['info'] = '请将信息填写完全';
					$this->ajaxReturn($data);
				}
			}
			//dump($id);die;
			$re = M('Member')->where("id={$id}")->setField($post);
			if($re){
				$data['status'] = 1;
				$data['info'] = '修改成功';
				$this->ajaxReturn($data);
			}else{
				$data['status'] = -3;
				$data['info'] = '修改失败';
				$this->ajaxReturn($data);
			}
		}else{
			$data['status'] = -1;
			$data['info'] = '网络异常';
			$this->ajaxReturn($data);
		}
		
	}
	public function setJifen(){
		$userId = I('id');
		$jifen  = I('jifen');
		$openid = M('Member')->where('id ='.$userId)->find();
		$r = M('Member')->where('id ='.$userId)->setInc('jifen',$jifen);
		if($r){
		    $this->addFinance($openid['openid'],1,2,$jifen,"奖励积分");
			$this->ajaxReturn(array('status'=>1));
		}else{
		    $this->ajaxReturn(array('status'=>-1));
		}
	}
  
  public function setYue(){
		$userId = I('id');
		$jifen  = I('jifen');
		$openid = M('Member')->where('id ='.$userId)->find();
		$r = M('Member')->where('id ='.$userId)->setInc('money',$jifen);
		if($r){
		    $this->addFinance($openid['openid'],1,1,$jifen,"管理员余额充值");
			$this->ajaxReturn(array('status'=>1));
		}else{
		    $this->ajaxReturn(array('status'=>-1));
		}
	}
	public function setShop(){
	    $userId = I('id');
	    $openid = M('Member')->where('id ='.$userId)->find();
	    if($openid['type'] == 1){
	    	$type = 0;
	    }
	    if($openid['type'] == 0){
	        $type = 1;
	    }
	    $r = M('Member')->where('id ='.$userId)->setField('type',$type);
	    if($r){
	        $this->ajaxReturn(array('status'=>1));
	    }else{
	        $this->ajaxReturn(array('status'=>-1));
	    }
	}
}
	
